﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    Layout = null;
}
<link href="~/lib/layui/css/layui.css" rel="stylesheet" />
@* <script src="~/lib/layui/layui.js"></script> *@
<script src="~/js/js.cookie.js"></script>
<script src="~/js/moment.js"></script>
<script src="~/lib/jquery/dist/jquery.js"></script>

@* 导出引入文件 *@
<title>Layui with XLSX</title>
<!-- 引入Layui的CSS -->
@* <link rel="stylesheet" href="path/to/layui.css"> *@
<!-- 引入XLSX库的CDN -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.17.0/xlsx.full.min.js"></script>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>生产退料</title>
    <link href="//unpkg.com/layui@2.9.21-rc.1/dist/css/layui.css" rel="stylesheet">
</head>
<body>
    <!-- 查询条件 -->
    <form class="layui-form" action="">
        <div class="layui-form">
            <div class="layui-form-item">
                <div class="layui-input-group">

                    <!-- 退料单编号 -->
                    <div class="layui-input-prefix">
                        <b>退料单编号</b>
                    </div>
                    <div>
                        <input id="strippingNode" type="text" placeholder="请输入退料单编号" class="layui-input" autocomplete="off" lay-affix="clear">
                    </div>

                    <!-- 生产工单 -->
                    <div class="layui-input-prefix" tyle="left:50px">
                        <b>生产工单</b>
                    </div>
                    <div>
                        <input id="productionWorkOrdersNo" type="text" placeholder="请输入生产工单" class="layui-input" autocomplete="off" lay-affix="clear">
                    </div>

                    <!-- 仓库名称 -->
                    <div class="layui-input-prefix" tyle="left:50px">
                        <b>仓库名称</b>
                    </div>
                    <div>
                        <input id="stashName" type="text" placeholder="请输入仓库名称" class="layui-input" autocomplete="off" lay-affix="clear">
                    </div>

                    <!-- 按钮 -->
                    <div class="layui-btn-container layui-col-xs12">
                        <button class="layui-btn" lay-submit lay-filter="demo-table-search">查询</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </div>
        </div>
    </form>
    <br />
    <div>
        <button onclick="addAll()" id="searchBtn" type="button" class="layui-btn layui-btn-normal layui-icon-addition">新增</button>
        <button type="button" class="layui-btn layui-icon-edit" layui-btn-green " >修改</button>
        <button type="button" class="layui-btn layui-bg-red layui-icon-delete">删除</button>
        <button type="button" class="layui-btn layui-bg-orange">导出</button>
    </div>
    <!-- 表格 -->
    <div>
        <table class="layui-table">
            <colgroup>
                <col width="80">
                <col width="130">
                <col width="130">
                <col width="130">
                <col width="130">
                <col width="130">
                <col width="130">
                <col width="130">
                <col width="130">
                <col width="130">
                <col>
            </colgroup>
            <thead>
                <tr>
                 @*    <th style="text-align:center"><input type="checkbox" name="check" onclick="checkAll()" /></th> *@
                    <th style="text-align:center">退料单编号</th>
                    <th style="text-align:center">退料单名称</th>
                    <th style="text-align:center">生产工单</th>
                    <th style="text-align:center">仓库名称</th>
            @*         <th style="text-align:center">库区名称</th>
                    <th style="text-align:center">库位名称</th> *@
                    <th style="text-align:center">退料日期</th>
                    <th style="text-align:center">单据状态</th>
                    <th style="text-align:center">操作</th>
                </tr>
            </thead>
            <tbody style="text-align:center">
            </tbody>
        </table>
        <div id="demo-laypage-all"></div>
    </div>

    <!-- HTML Content -->
    <script src="//unpkg.com/layui@2.9.21-rc.1/dist/layui.js"></script>
    <script>   
        layui.use(['form', 'laydate', 'util', 'table'], function () {
            var form = layui.form;
            var laypage = layui.laypage;
            var layer = layui.layer;
            var $ = layui.$;
            var laydate = layui.laydate;
            var util = layui.util;
            //分页
            var pageIndex = 1;
            var pageSize = 5;
            var strippingNode="";
            var productionWorkOrdersNo = "";
            var stashName = "";

            //就绪函数
            $(function () {
                StrippingLists(pageIndex, pageSize); //生产退料 显示 
            })

            // 搜索提交
            form.on('submit(demo-table-search)', function (data) {
                StrippingLists(pageIndex, pageSize);
                return false; // 阻止默认 form 跳转
            });
            //生产退料 显示  
            function StrippingLists(pageIndex, pageSize) {
                console.log(pageIndex, pageSize);
                // console.log(info);
                $.post({
                   url: '@ViewBag.FoundationWrite' + '/api/ProductType/StrippingsLists',
                    //url: "http://localhost:5292/api/ProductType/StrippingsLists",
                    data: JSON.stringify({
                        pageIndex: pageIndex,
                        pageSize: pageSize,
                        strippingNode: $("#strippingNode").val(),
                        productionWorkOrdersNo: $("#productionWorkOrdersNo").val(),
                        stashName: $("#stashName").val(),
                    }),
                    async: false,
                    dataType: "json",
                    contentType: "application/json",
                    success: res => {
                        // console.log(res);
                        var cont = ""; //定义变量

                        $.each(res.data, function () {
                            cont += `<tr>
                              
                               <td><span style="color:#1e9fff"><b>${this.strippingNode}</b></span></td>
                               <td>${this.strippingName}</td>
                               <td>${this.productionWorkOrdersNo}</td>
                               <td>${this.stashName}</td>
                       
                               <td>${this.strippingTime.replace("T", " ").substring(0, 19)}</td>
                               <td>${this.documents === 1 ? '草稿' : (this.documents === 2 ? '已确认' : (this.documents === 3 ? '审批中' : (this.documents === 4 ? '已审批' : '已完成')))}</td>
                               <td>
                                         <a onclick="StrippingAlls('${this.strippingNode}')" style="color:#ff5722"">执行退料</a>
                                        <a onclick="Updates('${this.strippingId}','${this.strippingNode}')" style="color:green"">修改</a>
                                        <a onclick="Steat(0)" style="color:cornflowerblue">审批通过</a>
                                        <a onclick="Steat(1)" style="color:cornflowerblue">审批不通过</a>
                                       </td>
                            </tr>`
                        })
                        $("tbody").html(cont);
                        console.log(res);
                        // console.log(996, pageSize, pageIndex, res)
                        // 完整显示
                        laypage.render({
                            elem: 'demo-laypage-all', // 元素 id
                            count: res.totalCount, // 数据总数
                            curr: pageIndex,//当前页
                            limit: pageSize,//每页几条     
                            limits: [1, 2, 3], 
                            layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'], // 功能布局
                            jump: function (obj, first) {
                                console.log(obj.curr);
                                console.log(obj.limit);
                                if (!first) {
                                    
                                    StrippingLists(obj.curr, obj.limit);
                                }
                            }
                        });
                        //        // <td>${}</td>
                        // <td>${}</td>
                        // <td><input type="checkbox" class="check" value="${this.strippingId}" onclick="checkfan()"/></td>
                    }
                });
            };
          
            //导出
            function exportTableToExcel() {
                var table = document.querySelector("table.layui-table tbody");
                var ws = XLSX.utils.table_to_sheet(table);
                var wb = XLSX.utils.book_new();
                XLSX.utils.book_append_sheet(wb, ws, "Sheet1");
                XLSX.writeFile(wb, "工作中心列表.xlsx");
            }

            document.querySelector(".layui-btn.layui-bg-orange").addEventListener("click", exportTableToExcel);
        });

        //修改生产退料
        function Updates(strippingId, strippingNode) {
            localStorage["strippingNode"] = strippingNode;
            console.log(strippingId, strippingNode);
            localStorage["strippingId"] = strippingId; 
            location.href = "/Messages/StrippingUpdate?StrippingId=" + strippingId;
        };

        //跳转新增
        function addAll() {
            location.href = "/Messages/StrippingCreate";
        }
 
        //执行退料
        function StrippingAlls(strippingNode) {
            var datec = new Date()
            var year = datec.getFullYear();
            var month = (datec.getMonth() + 1).toString().padStart(2, "0")
            var date = (datec.getDate() + 1).toString().padStart(2, "0")
            var hours = datec.getHours()
            var minute = (datec.getMinutes()).toString().padStart(2, "0")
            var seconds = (datec.getSeconds()).toString().padStart(2, "0")
            var result = "ISSUE" + year + month + date + hours + minute + seconds;

            //执行退料友好提示
            if (confirm("确定要执行退料吗?")) {
                var data = {
                    strippingNode: strippingNode,
                }
                $.ajax({
                    url: '@ViewBag.FoundationWrite' + "/api/ProductType/PerformStrips",
                    data: JSON.stringify(data),
                    type: 'post',
                    async: false,
                    dataType: 'json',
                    contentType: 'application/json',
                    success: res => {
                        if (res.code > 0) {
                            alert("执行退料成功!");
                            location.reload();
                        }
                        else {
                            alert("执行退料失败!");
                        }
                    }
                });
            }

        }

        //审批
        function Steat(strippingStatus) {
            var selectedCheckboxes = $(': checkbox: gt(0): checked');
            if (selectedCheckboxes.length == 0) {
                1ayer.msg('请选择要审核的记录’);
                                return;
            }
            var ids = "";
            selectedCheckboxes.each(function () {
                ids += $(this).va1()
            })
            ids = ids.substring(0, ids.length - 1);
            $.post({
                url: "",
                data: JSON.stringify({
                    strippingId: ids,
                    strippingStatus: strippingStatus
                }),
                async: false,
                dataType: "json",
                contentType: "application/json",
                success: res => {
                    if (res.code > 0) {
                        alert("修改成功!");
                        StrippingLists(pageIndex, pageSize);
                    }
                    else if (res.data.strippingStatus == 1 || res.data.strippingStatus == 0) {
                        alert("已审批，请勿重复审批!");
                    }
                    else {
                        alert("修改成失败!");
                    }
                }
            })
        
        }
    </script>
</body>
</html>